package com.atguigu.cloud.controller;

import com.atguigu.cloud.service.FlowLimitService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController {
    @GetMapping("/testA")
    public String testA(){
        return "testA";
    }

    @GetMapping("/testB")
    public String tsetB(){
        return "testB";
    }

    /**流控-链路演示demo
     * C和D两个请求都访问flowLimitService.common()方法，阈值到达后对C限流，对D不管
     */
    @Resource
    private FlowLimitService flowLimitService;
    @GetMapping("/testC")
    public String testC(){
        flowLimitService.common();
        return "testC";
    }

    @GetMapping("/testD")
    public String testD(){
        flowLimitService.common();
        return "testD";
    }

    @GetMapping("/testE")
    public String testE(){
        System.out.println(System.currentTimeMillis()+"testE");
        return "testE";
    }

    @GetMapping("/testF")
    public String testF(){
        //暂停几秒钟线程
        try { TimeUnit.SECONDS.sleep(1); }
        catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println("----测试:新增熔断规则-慢调用比例 ");
        return "------testF 新增熔断规则-慢调用比例";
    }

    @GetMapping("/testG")
    public String testG(){
        System.out.println("----测试:新增熔断规则-异常比例 ");
        int age = 10/0;
        return "------testG,新增熔断规则-异常比例 ";
    }

    @GetMapping("/testH")
    public String testH(){
        System.out.println("----测试:新增熔断规则-异常数 ");
        ArrayList<String> list = new ArrayList<>();
        list.get(1);
        return "------testG,新增熔断规则-异常数 ";
    }


}
